class WordsController < ApplicationController

  def look_up
    render :nothing => true and return unless request.xhr?

    @pattern = Word.new(params[:word])
    @strategy = Strategy.find_by_id(params[:strategy_id])

    if @pattern.nil? || @pattern.text.to_s.blank?
      @div = :matches
      @class_name = Match.class_name
      render :template => 'shared/error' and return
    end

    @matches = Word.match(@pattern, @strategy,
                          :view_all => (params[:view_all] == true.to_s))
  end

  def show
    @word = Word.find_by_id(params[:id], :include => :word_definitions)

    unless @word
      flash[:error] = 'Word not found!'
      redirect_to :back and return
    end

    if @word.word_definitions.empty?      
      @word.word_definitions = WordDefinition.for(@word, nil, 
                                                  :force_look_up => true,
                                                  :limit => 1)
    end
  end

  def back_to
    render :nothing => true and return unless request.xhr?

    @word = Word.find_by_id(params[:id])
    @word_definitions = @word ? WordDefinition.for(@word, nil, 
                                                    :page => params[:page], 
                                                    :force_look_up => true) : []
  end

end
